package bb.lanxing.service;

import static bb.lanxing.model.database.PostQueue.TYPE_LUSHU_COMMENT;
import static bb.lanxing.model.database.PostQueue.TYPE_TOPIC;
import static bb.lanxing.model.database.PostQueue.TYPE_WORKOUT_COMMENT;

import android.app.IntentService;
import android.app.NotificationManager;
import android.content.Context;
import android.content.Intent;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;

import bb.lanxing.R;
import bb.lanxing.activity.sport.SportRecordDetailActivity;
import bb.lanxing.manager.RemoteServiceManager;
import bb.lanxing.model.database.PostQueue;
import bb.lanxing.model.database.Workout;
import bb.lanxing.model.json.RouteBookComment;
import bb.lanxing.model.json.Topic;
import bb.lanxing.model.json.WorkoutComment;
import bb.lanxing.mvp.presetner.PostQueuePresenterImpl;
import bb.lanxing.mvp.presetner.i.IPostQueuePresenter;
import bb.lanxing.mvp.view.IPostQueueView;
import bb.lanxing.activity.routebook.RouteBookInfoActivity;
import bb.lanxing.util.text.TextUtils;

public class PostQueueService extends IntentService implements IPostQueueView {
    public static final String ACTION_SEND_POST_FAILED = "bb.lanxing.PostQueue-failed";
    public static final String ACTION_SEND_POST_SUCCEED = "bb.lanxing.PostQueue-succeed";
    public static final String EXTRA_DELETE = "PostQueue-delete";
    public static final String EXTRA_POST_DATA = "PostQueue-data";
    public static final String EXTRA_SEND_FAILED = "PostQueue-failed";
    public static final String TAG_NOTIFICATION = "PostQueue-notification";
    private final IPostQueuePresenter postQueuePresenter;

    public static void sendData(Context context, PostQueue data, ArrayList<String> imagePaths) {
        Intent intent = new Intent(context, PostQueueService.class);
        intent.putExtra(EXTRA_POST_DATA, data);
        intent.putStringArrayListExtra("image_paths", imagePaths);
        context.startService(intent);
    }

    public static void deleteData(Context context, PostQueue data) {
        Intent intent = new Intent(context, PostQueueService.class);
        intent.putExtra(EXTRA_POST_DATA, data);
        intent.putExtra(EXTRA_DELETE, true);
        context.startService(intent);
    }

    public static void checkAndSend(Context context, boolean force, boolean reset) {
        if (force || !RemoteServiceManager.getInstance().isSporting()) {
            Intent intent = new Intent(context, PostQueueService.class);
            intent.putExtra("reset", reset);
            context.startService(intent);
        }
    }

    public PostQueueService() {
        super("post-queue");
        this.postQueuePresenter = new PostQueuePresenterImpl(this);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        if (intent == null) {
            return;
        }
        PostQueue postQueue = intent.getParcelableExtra(EXTRA_POST_DATA);
        if (postQueue == null) {
            if (intent.getBooleanExtra("reset", false)) {
                this.postQueuePresenter.resetSendingState();
            }
            this.postQueuePresenter.sendPostQueue();
        } else if (intent.getBooleanExtra(EXTRA_DELETE, false)) {
            this.postQueuePresenter.deletePost(postQueue);
        } else if (this.postQueuePresenter.isValid(postQueue)) {
            ArrayList<String> stringArrayListExtra = intent.getStringArrayListExtra("image_paths");
            postQueue.setCreateTime(System.currentTimeMillis());
            postQueue.save();
            if (stringArrayListExtra != null && !stringArrayListExtra.isEmpty()) {
                this.postQueuePresenter.cacheImage(postQueue, stringArrayListExtra);
            }
            this.postQueuePresenter.sendPost(postQueue);
        }
    }

    @Override
    public void onSendSucceed(PostQueue data) {
        clearNotify(this, data.getId().intValue());
        sendBroadcast(new Intent(ACTION_SEND_POST_SUCCEED).putExtra(EXTRA_POST_DATA, data));
    }

    @Override
    public void onSendFailed(PostQueue postQueue) {
        String string = "";
        Intent intent = null;
        sendBroadcast(new Intent(ACTION_SEND_POST_FAILED).putExtra(EXTRA_POST_DATA, postQueue));
        String string2 = getString(R.string.post_queue_send_failed);
        int type = postQueue.getType();
        if (type == TYPE_TOPIC) {
            string = getString(R.string.post_queue_send_failed_notification_topic, TextUtils.getLimitString((JSON.parseObject(postQueue.getContent(), Topic.class)).getTitle(), 5));

        } else if (type == TYPE_WORKOUT_COMMENT) {
            string = getString(R.string.post_queue_send_failed_notification_comment, TextUtils.getLimitString(JSON.parseObject(postQueue.getContent(), WorkoutComment.class).getContent(), 5));
            Workout workout = new Workout();
            workout.setServerId(postQueue.getSubId());
            intent = new Intent(getApplicationContext(), SportRecordDetailActivity.class);
            intent.putExtra("workoutId", workout.getId());
        } else if (type == TYPE_LUSHU_COMMENT) {
            string = getString(R.string.post_queue_send_failed_notification_comment, TextUtils.getLimitString(JSON.parseObject(postQueue.getContent(), RouteBookComment.class).getContent(), 5));
            intent = new Intent(getApplicationContext(), RouteBookInfoActivity.class);
            intent.putExtra("lushu_server_id", postQueue.getSubId());
        }
    }

    public static void clearNotify(Context context, int id) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (notificationManager != null) {
            notificationManager.cancel(TAG_NOTIFICATION, id);
        }
    }
}